AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "Airboat_engine_idle" )
util.PrecacheSound( "Airboat_engine_stop" )
util.PrecacheSound( "apc_engine_start" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Pressure_Increment = 80
local Energy_Increment = 10

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.overdrive = 0
	self.overdrivefactor = 0
	self.damaged = 0
	self.lastused = 0
	LS_RegisterEnt(self.Entity)

	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On", "Overdrive" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(5500)

	end
end

function ENT:Setup()
	self:TriggerInput("On", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Entity:EmitSound( "Airboat_engine_idle" )
				self.Active = 1
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		else
			if ( self.Active == 1 ) then
				self.Entity:StopSound( "Airboat_engine_idle" )
				self.Entity:EmitSound( "Airboat_engine_stop" )
				self.Entity:StopSound( "apc_engine_start" )
				self.Active = 0
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		end
	elseif (iname == "Overdrive") then
		if (value > 0) then
			if ( self.Active == 1 ) then
				self.Entity:StopSound( "Airboat_engine_idle" )
				self.Entity:EmitSound( "Airboat_engine_idle" )
				self.Entity:EmitSound( "apc_engine_start" )
			end
			self.overdrive = 1
			self.overdrivefactor = value
		else
			if ( self.Active == 1 ) then
				self.Entity:StopSound( "Airboat_engine_idle" )
				self.Entity:EmitSound( "Airboat_engine_idle" )
				self.Entity:StopSound( "apc_engine_start" )
			end
			self.overdrive = 0
			self.overdrivefactor = 0
		end
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
	if ((self.Active == 1) and (math.random(1, 10) <= 4)) then
		self.Active = 0
		self.Entity:StopSound( "Airboat_engine_idle" )
		self.Entity:EmitSound( "Airboat_engine_stop" )
		self.Entity:StopSound( "apc_engine_start" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", self.Active) end
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local function Func1(Entity)
		if Entity:IsValid() then
			local Effect = EffectData()
				Effect:SetOrigin(Entity:GetPos() + Vector( math.random(-60, 60), math.random(-60, 60), math.random(-60, 60) ))
				Effect:SetScale(1)
				Effect:SetMagnitude(25)
				util.Effect("Explosion", Effect, true, true)
		end
	end
	local function Func2(Entity)
		if Entity:IsValid() then
			local Effect = EffectData()
				Effect:SetOrigin(Entity:GetPos())
				Effect:SetScale(3)
				Effect:SetMagnitude(100)
				util.Effect("Explosion", Effect, true, true)

				Entity:Remove()
		end
	end
	timer.Simple(1, Func1, self.Entity)
	timer.Simple(1.2, Func1, self.Entity)
	timer.Simple(2, Func1, self.Entity)
	timer.Simple(2, Func2, self.Entity)
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	self.Entity:StopSound( "Airboat_engine_idle" )
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
end


function ENT:Use()
end

function ENT:Output()
	return 1
end

function ENT:Pump_Air()
	local energy = RD_GetResourceAmount(self, "energy")
	local inc = Pressure_Increment
	local eng = Energy_Increment
	if self.overdrive > 0 then
		eng = math.Round(self.overdrive*Energy_Increment)
	end
	if (energy >= eng) then
		if ( self.overdrive > 0 ) then
			inc = math.Round(inc * self.overdrivefactor)
			if self.overdrivefactor > 3 then
				self:Destruct()
			else
				if self.overdrive > 1 then
					DamageLS(self, math.random(0, math.Round(self.overdrivefactor*3)))
				end
			end
		end
		
		RD_ConsumeResource(self, "energy", eng)
		RD_SupplyResource(self, "air", inc)
	else
		self.Active = 0
		self.Entity:StopSound( "Airboat_engine_idle" )
		self.Entity:EmitSound( "Airboat_engine_stop" )
		self.Entity:StopSound( "apc_engine_start" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
	end
		
	return
end

function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "Air Compressor\n(OFF)\nEnergy: " .. self.val1 )
	else
		if ( self.overdrive > 0 ) then
			self:SetOverlayText( "Air Compressor\n(OVERDRIVE)\nEnergy: " .. self.val1 )
		else
			self:SetOverlayText( "Air Compressor\n(ON)\nEnergy: " .. self.val1 )
		end
	end
	if ( self.timer > CurTime() ) then return end
	self.val1 = RD_GetResourceAmount(self, "energy")
	if ( self.Active == 1 ) then
		if (self.environment.inwater == 1) then
			self.Entity:SetColor(50, 50, 50, 255)
			self.Entity:StopSound( "Airboat_engine_idle" )
			self.Entity:EmitSound( "Airboat_engine_stop" )
			self.Entity:StopSound( "apc_engine_start" )
			self.Active = 0
			self:Destruct()
		end
		if (self.environment.habitat == 0) then
			self.Entity:StopSound( "Airboat_engine_idle" )
			self.Entity:EmitSound( "Airboat_engine_stop" )
			self.Entity:StopSound( "apc_engine_start" )
			self.Active = 0
			return
		end
	end
	if ( self.Active == 1 ) then self:Pump_Air() end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Entity:EmitSound( "Airboat_engine_idle" )
			self.Active = 1
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", self.Active) end
		else
			self.Active = 0
			self.Entity:StopSound( "Airboat_engine_idle" )
			self.Entity:EmitSound( "Airboat_engine_stop" )
			self.Entity:StopSound( "apc_engine_start" )
		end
	end
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
